package cn.wygandwdn.common.message;

import java.util.Arrays;

/**
 * @description:    Rpc请求体,通过group+version的形式解决一个接口多个实现的问题
 * @author: WYG
 * @time: 2021/10/24 20:19
 */

public class RpcRequest extends Message {

    /**
     * 远程调用接口名称
     */
    private String interfaceName;
    /**
     * 远程调用方法名称
     */
    private String methodName;
    /**
     * 远程调用方法参数类型
     */
    private Class<?>[] parametersTypes;
    /**
     * 远程调用方法参数实际值
     */
    private Object[] parameters;
    /**
     * 远程调用方法分组
     */
    private String group;
    /**
     * 远程调用方法版本
     */
    private String version;

    public RpcRequest() {}

    public RpcRequest(Builder builder) {
        this.interfaceName = builder.interfaceName;
        this.methodName = builder.methodName;
        this.parametersTypes = builder.parametersTypes;
        this.parameters = builder.parameters;
        this.group = builder.group;
        this.version = builder.version;
        super.setSequenceId(builder.sequenceId);
    }

    public String getInterfaceName() {
        return interfaceName;
    }

    public void setInterfaceName(String interfaceName) {
        this.interfaceName = interfaceName;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public Class<?>[] getParametersTypes() {
        return parametersTypes;
    }

    public void setParametersTypes(Class<?>[] parametersTypes) {
        this.parametersTypes = parametersTypes;
    }

    public Object[] getParameters() {
        return parameters;
    }

    public void setParameters(Object[] parameters) {
        this.parameters = parameters;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getServiceName() {
        return interfaceName + "$" + group + "$" + version;
    }

    @Override
    public int getMessageType() {
        return RpcRequestMessage;
    }

    @Override
    public String toString() {
        return "RpcRequest{" +
                "interfaceName='" + interfaceName + '\'' +
                ", methodName='" + methodName + '\'' +
                ", parametersTypes=" + parametersTypes +
                ", parameters=" + Arrays.toString(parameters) +
                ", group='" + group + '\'' +
                ", version='" + version + '\'' +
                '}';
    }

    public static class Builder {
        private String interfaceName;
        private String methodName;
        private Class<?>[] parametersTypes;
        private Object[] parameters;
        private String group;
        private String version;
        private long sequenceId;

        public Builder(String interfaceName) {
            this.interfaceName = interfaceName;
        }

        public Builder method(String val) {
            this.methodName = val;
            return this;
        }

        public Builder paramType(Class<?>[] val) {
            this.parametersTypes = val;
            return this;
        }

        public Builder param(Object[] val) {
            this.parameters = val;
            return this;
        }

        public Builder group(String val) {
            this.group = val;
            return this;
        }

        public Builder version(String val) {
            this.version = val;
            return this;
        }

        public Builder sequenceId(long sequenceId) {
            this.sequenceId = sequenceId;
            return this;
        }

        public RpcRequest build() {
            return new RpcRequest(this);
        }
    }
}
